#ifndef CVIEW_HPP
#define CVIEW_HPP

#include <time.h>
#include <shlobj.h>
#include <shtypes.h>
#include <windows.h>
#include <strsafe.h>
#include <commctrl.h>

class CView
{
public:
    CView() {}
    ~CView() {}
    CView(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_hWnd = NULL;
        m_hParent = NULL;
        m_wcex = {0};
        m_rcMenuBar = {0};
        m_rcToolBar = {0};
        Register(lpszClassName, hInstance);
    }
    CView(const CView &) = delete;
    CView &operator=(const CView &) = delete;
    HWND GetHWND() const
    {
        return m_hWnd;
    }
    void SetToolBarRect(RECT rcToolBar)
    {
        m_rcToolBar = rcToolBar;
    }
    void SetMenuBarRect(RECT rcMenuBar)
    {
        m_rcMenuBar = rcMenuBar;
    }
    LRESULT Register(LPCTSTR lpszClassName, HINSTANCE hInstance)
    {
        m_wcex = {0};
        m_wcex.cbSize = sizeof(WNDCLASSEX);
        m_wcex.cbClsExtra = 0;                                      // 类的额外内存
        m_wcex.cbWndExtra = 0;                                      // 窗口额外内存
        m_wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 背景
        m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);               // 光标 第一个为NULL代表使用系统提供的光标
        m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);             // 图标 第一个为NULL代表使用系统提供的图标
        m_wcex.hInstance = hInstance;                               // 应用程序实例句柄
        m_wcex.lpfnWndProc = CView::WndProc;                        // 回调函数
        m_wcex.lpszClassName = lpszClassName;                       // 窗口类名称
        m_wcex.lpszMenuName = NULL;                                 // 菜单名称
        m_wcex.style = CS_HREDRAW | CS_VREDRAW;                     // 显示风格
        m_hInstance = hInstance;
        if (!RegisterClassEx(&m_wcex))
        {
            MessageBox(NULL, TEXT("Failed to register child window class"), TEXT("Error"), MB_OK | MB_ICONERROR);
            return -1;
        }
        return 0;
    }
    LRESULT Create(HWND hParent, LPRECT lpRect, LPCTSTR szWindowName)
    {
        // Create the child window
        RECT rect;
        if (!lpRect)
        {
            GetClientRect(hParent, &rect);
            lpRect = &rect;
        }
        m_nMenuBarHeight = m_rcMenuBar.bottom - m_rcMenuBar.top;
        m_nToolBarHeight = m_rcToolBar.bottom - m_rcToolBar.top;
        lpRect->top += m_nToolBarHeight;
        m_hWnd = CreateWindow(m_wcex.lpszClassName, szWindowName, WS_CHILD | WS_VISIBLE,
                              lpRect->left, lpRect->top, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top,
                              hParent, NULL, m_hInstance, this);
        m_hParent = hParent;
        SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
        return 0;
    }
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        CView *pThis = (CView *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
        if (pThis)
        {
            return pThis->WindowProc(hWnd, uMsg, wParam, lParam);
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0));
            HPEN holdPen = (HPEN)SelectObject(hdc, hPen);
            HBRUSH hBrush = CreateSolidBrush(RGB(200, 255, 200));
            HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);

            RECT rect;
            GetClientRect(hWnd, &rect);
            Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);

            SelectObject(hdc, holdPen);
            SelectObject(hdc, holdBrush);
            DeleteObject(hPen);
            DeleteObject(hBrush);
            ReleaseDC(hWnd, hdc);
            EndPaint(hWnd, &ps);
            break;
        }
        case WM_COMMAND:
        {
            TCHAR szTitle[MAX_PATH] = {0};
            StringCchPrintf(szTitle, 256, TEXT("我收到了: %d"), LOWORD(wParam));
            // MessageBox(0, szTitle, TEXT("我收到了"), MB_OK);
            break;
        }
        case WM_SIZE:
        {
            RECT rcParent;
            GetClientRect(m_hParent, &rcParent);
            rcParent.top += m_rcToolBar.bottom - m_rcToolBar.top;
            MoveWindow(m_hWnd, rcParent.left, rcParent.top, rcParent.right, rcParent.bottom, TRUE);
            InvalidateRect(hWnd, NULL, FALSE);
            break;
        }
        case WM_MOUSEMOVE:
        {
            int xPos = LOWORD(lParam);
            int yPos = HIWORD(lParam);
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("xPos: %d, yPos: %d"), xPos, yPos);
            // SetWindowText(m_hParent, buffer);
            break;
        }
        case WM_CLOSE:
        {
            DestroyWindow(hWnd);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            break;
        }
        return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
    }

public:
    UINT m_nMenuBarHeight;
    UINT m_nToolBarHeight;
    HWND m_hWnd;
    HWND m_hParent;
    WNDCLASSEX m_wcex;
    RECT m_rcMenuBar;
    RECT m_rcToolBar;
    HINSTANCE m_hInstance;
};

#endif